package com.yunzone.cloud.modules.payment.controller;

import com.yunzone.cloud.common.entity.Result;
import com.yunzone.cloud.modules.payment.entity.Payment;
import com.yunzone.cloud.modules.payment.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Payment)表控制层
 *
 * @author makejava
 * @since 2020-04-02 08:49:30
 */
@RestController
@RequestMapping("/payment")
@Slf4j
public class PaymentController {
    /**
     * 服务对象
     */
    @Resource
    private PaymentService paymentService;
    @Value("${server.port}")
    private String serverPort;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/query/{id}")
    public Result<Payment> selectOne(@PathVariable("id") Long id) {
        Payment payment=this.paymentService.queryById(id);
        log.info("操作成功@,{}",payment);
        return new Result(200,true,"查询成功，服务处理来自:"+serverPort,payment);
    }

    /**
     * 插入数据
     *
     * @param payment 对象
     * @return Result data是ID
     */
    @PostMapping("/add")
    public Result selectOne(@RequestBody Payment payment) {
        try {
            payment=paymentService.insert(payment);
            log.info("支付插入成功,{}",payment);
            return new Result(200,true,"插入成功,服务处理来自:"+serverPort,payment.getId());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(500,false,"插入失败,服务处理来自:"+serverPort,payment.getId());
        }

    }

    @Resource
    private DiscoveryClient discoveryClient;
    @GetMapping("/discovery")
    public Object discovery(){
        List<String> services=discoveryClient.getServices();
        for(String service:services){
            log.info("服务内容：{}",service);
        }
        System.out.println("-------------------");
        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
        for (ServiceInstance instance : instances) {
            log.info("{}--{}--{}--{}",instance.getServiceId(),instance.getHost(),instance.getPort(),instance.getUri());
        }
        return discoveryClient;
    }

}